//
//  XYVisitorLoginView.swift
//  01-新浪微博
//
//  Created by 陈许亚 on 16/3/6.
//  Copyright © 2016年 cxy. All rights reserved.
//

import UIKit
import SnapKit

//声明协议 ,需要继承基协议
//OC中有多继承么 如果没有 用什么替代
// optional 还是OC中的关键字 如果在Swift中要使用OC中的一些机制 或者特性 需要在前加入@objc告诉编译器该声明是兼容OC的
/**
*  协议三部曲
1.声明协议
protocol+类名Delegate: NSObjectProtocol{

//协议方法，如果要是可选择实现的在协议方法前用optional修饰，并且还要在声明协议之前用@objc来修饰
}
2、声明代理对象：
weak var 代理名称 ：类名Delegate
3.在合适的地方让代理去做事情 -------代理.协议方法

代理方：
1.设置代理，遵守协议
2.代理去实现协议中方法


*/
//声明协议
protocol XYVisitorLoginViewDelegate: NSObjectProtocol {
    //用户登录协议方法
    func userWillLogin()
    //用户注册协议方法
    func userWillRegeist()
}



class XYVisitorLoginView: UIView {
    
    //声明代理对象
    weak var visitorLoginViewDelegate: XYVisitorLoginViewDelegate?

    override init(frame: CGRect) {

        super.init(frame: frame)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
        
    }
    
        
    //动画方法
   private  func startAnimation(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        //设置动画持续时间
        anim.duration = 20
        //当动画效果完成/ 视图处于非活跃状态下 动画效果不移除图层
        anim.removedOnCompletion = false
        //将基础动画添加到图层
        circleIcon.layer.addAnimation(anim, forKey: nil)
        
    }
    //MARK :自定义访客视图
    private func setUpUI(){
        addSubview(circleIcon)
        addSubview(backImage)
        addSubview(largeHouse)
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(regeistBtn)
        //布局 需要相对于一个已经约束好的视图来添加 链式布局
      circleIcon.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
        }
        backImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
            
        }
        
      largeHouse.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(circleIcon.snp_center)
        
        }
        
     tipLable.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(circleIcon.snp_centerX)
        make.top.equalTo(circleIcon.snp_bottom).offset(16)
        make.width.equalTo(230)
        make.height.equalTo(40)

        }
        
     loginBtn.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(tipLable.snp_bottom).offset(16)
        //make.left.equalTo(tipLable.snp_left)
        make.centerX.equalTo(circleIcon.snp_centerX).offset(-65)
        make.width.equalTo(100)

        }
        
     regeistBtn.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(tipLable.snp_bottom).offset(16)
        make.centerX.equalTo(circleIcon.snp_centerX).offset(65)
        make.width.equalTo(100)
        
        }
        
        let value: CGFloat = 236.0 / 255.0
        //可以用数码测色计来吸取渐变图片的颜色，把view的背景色设置为该颜色，看上去就和谐了
        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //MARK:按钮的点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        regeistBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
        
        
    }
    //MARK:登录按钮
    func loginBtnDidClick(){
        
        visitorLoginViewDelegate?.userWillLogin()
    
    }
    
    //MARK:注册按钮
    func registerBtnDidClick(){
    
        visitorLoginViewDelegate?.userWillRegeist()
    }
    
    
    //MARK:设置页面信息
    func setUpInfoWith(tipText: String,imageName: String?){
        tipLable.text = tipText
        
        if imageName != nil{
        //说明传过来的有图片
            circleIcon.image  = UIImage(named: imageName!)
            largeHouse.hidden = true
            self.bringSubviewToFront(circleIcon)

        }else {
            //开始动画
            startAnimation()

        }
        
    }

    
    //MARK : 懒加载子视图
    //backImage
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //加载圆圈图标
    private lazy var circleIcon: UIImageView = {
        let circle = UIImageView()
        circle.image = UIImage(named: "visitordiscover_feed_image_smallicon")
//        startAnimation()
        return circle
    }()
    //UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //加载大房子
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //加载标签语
        private lazy var tipLable: UILabel = UIFactory.labelFactory("", textColor: UIColor.darkGrayColor(), fontSize: 14)
    

    //加载登录注册按钮
    private lazy var loginBtn: UIButton = UIFactory.buttonWithTitle("common_button_white_disable", title: "登录", titleColor: UIColor.darkGrayColor(), fontSizeOfTitle: 14)
    //TODO:老师的有背景图片拉伸，拉伸不变形方法，我的没有，自己再来写，看是为什么这样做
        
//    {
//        
//        let login = UIButton()
//        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        login.setTitle("登录", forState: .Normal)
//        login.setTitleColor(UIColor.blackColor(), forState: .Normal)
//        login.titleLabel?.font = UIFont.systemFontOfSize(14)
//        login.sizeToFit()
//        
//        return login
//    }()
   //注册按钮
    private lazy var regeistBtn: UIButton = UIFactory.buttonWithTitle("common_button_white_disable", title: "注册", titleColor: themeColor, fontSizeOfTitle: 14)
    //TODO:注册按钮标题默认是有颜色的，主题颜色
        
//    {        
//        let regeist = UIButton()
//        regeist.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        regeist.setTitle("注册", forState: .Normal)
//        regeist.setTitleColor(UIColor.blackColor(), forState: .Normal)
//        regeist.titleLabel?.font = UIFont.systemFontOfSize(14)
//        regeist.sizeToFit()
//        regeist.selected = true
//        regeist.setTitleColor(UIColor.orangeColor(), forState: .Selected)
//        return regeist
//    }()
    
    
    
    
    
    
}
